// pages/gwc/gwc.js
const {
  getCart,
  upnubmer,
  input
} = require('../../http/api')
const {
  getCartDel
} = require('../../http/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    isShow: true,
    isHidden: true,
    isIndex: undefined,
    num: 0,
    key: '',
    x: 0,
    ind: null,
  },

  // 点击去逛逛到首页
  toHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 改变单选框的选中状态
  ischeck(e) {
    let key = e.currentTarget.dataset.key
    let selected = e.currentTarget.dataset.selected
    console.log(key, selected)
    input(key, !selected,wx.getStorageSync('token')).then(res => {
      console.log(res.data.data)
      this.setData({
        cartList: res.data.data.items,
        num: res.data.data.price
      })
    })
  },
  start(e) {
    this.setData({
      x: e.changedTouches[0].pageX
    })
  },
  hend(e) {
    let inde = e.currentTarget.dataset.num
    let arr = this.data.cartList
    if (arr[inde].status == true) {
      arr.forEach((ele, index) => {
        if (index == inde) {
          ele.status = false
        }
      })
      this.setData({
        cartList: arr,
      })
    } else {
      if (this.data.x - e.changedTouches[0].pageX > 50) {
        arr.forEach((ele, index) => {
          if (index == inde) {
            ele.status = true
          }
        })
        // console.log(arr)
        this.setData({
          cartList: arr,
          ind: e.currentTarget.dataset.num
        })
        // console.log(this.data.cartList)
      }
    }
  },
  // 减数量
  jian(e) {
    let number = e.currentTarget.dataset.number - 1
    let key = e.currentTarget.dataset.key
    if (number >= 1) {
      upnubmer(key, number,wx.getStorageSync('token')).then(res => {

        console.log(res)
        this.setData({
          cartList: res.data.data.items,
          num: res.data.data.price
        })
        this.setnum(res.data.data.number)
      })
    } else {
      this.setData({
        key: key,
        isHidden: false
      })
    }
    this.setData({
      cartList: this.data.cartList
    })
  },
  // 加数量
  jia(e) {
    console.log(e.currentTarget.dataset)
    let number = e.currentTarget.dataset.number + 1
    let key = e.currentTarget.dataset.key
    upnubmer(key, number,wx.getStorageSync('token')).then(res => {

      console.log(res)
      this.setData({
        cartList: res.data.data.items,
        num: res.data.data.price
      })
      this.setnum(res.data.data.number)
    })
  },
  remove(e) {
    console.log(1);
    console.log(e)
    getCartDel(e.currentTarget.dataset.key,wx.getStorageSync('token')).then(res => {
      console.log(res);
      if (res.data.code == 700) {
        this.setData({
          cartList: [],
          num: 0,
          isShow: true,
        })
        this.setnum(0)
      } else {
        this.setData({
          cartList: res.data.data.items,
          num: res.data.data.price
        })
        this.setnum(res.data.data.number)
      }
    })
  },

  //删除商品
  del(e) {
    // console.log(this.data.key);
    getCartDel(this.data.key,wx.getStorageSync('token')).then(res => {

      if (res.data.code == 700) {
        this.setData({
          cartList: [],
          num: 0,
          isShow: true,
        })
        this.setnum(0)
      } else {
        this.setData({
          cartList: res.data.data.items,
          num: res.data.data.price
        })
        this.setnum(res.data.data.number)
      }
    })
    this.setData({
      isHidden: true,
    })
  },
  close() {
    this.setData({
      isHidden: true
    })
  },









  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  setnum(num){
    if(num<1){
      wx.removeTabBarBadge({
        index:3,
      })
    }else{
      wx.setTabBarBadge({
        index: 3,
        text:`${num}`,
        success:()=>{
          console.log(num)
        }
      })
    }
    
   
  },
  onShow: function () {
    //获取购物车数据
    getCart(wx.getStorageSync('token')).then(res => {
      console.log(res);
     
      if (res.data.data) {
        this.data.isShow = false
        let arr = res.data.data
        this.setData({
          cartList: arr.items,
          isShow: false,
          num: res.data.data.price
        })
          this.setnum(res.data.data.number)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})